package com.widget

import android.content.Context

import android.os.Bundle

import android.view.LayoutInflater
import android.view.View
import androidx.annotation.LayoutRes
import androidx.appcompat.app.AppCompatDialog
import androidx.databinding.DataBindingUtil
import androidx.databinding.ViewDataBinding
import com.aop.annotation.SingleClick


import com.helper.utils.SystemUtil
import com.sh.doctors.cloud.BR
import com.sh.doctors.cloud.R
import com.base.view.Presenter

/**
 * 界面描述：
 * <p>
 * Created by tianyang on 2017/11/3.
 */
abstract class BaseDialog<VB : ViewDataBinding> : AppCompatDialog, Presenter {
    protected lateinit var mBinding: VB
    protected lateinit var mContext: Context

    constructor(context: Context?) : this(context, R.style.DialogTheme)

    constructor(context: Context?, themeResId: Int) : super(context, themeResId) {
        mContext = context!!
    }


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
//        DeviceUtil.setStickFullScreen(this.window.decorView)

        mBinding = DataBindingUtil.inflate(LayoutInflater.from(mContext), getLayoutId(), null, false)
        mBinding.setVariable(BR.presenter, this)
        mBinding.executePendingBindings()
        setContentView(mBinding.root)

        setWidthAndHeight()

        //初始化视图
        initView()
        // 事件处理
        initEvent()
        // 加载数据
        loadData()
    }

    private fun setWidthAndHeight() {
        val p = window!!.attributes  //获取对话框当前的参数值
        p.width = SystemUtil.getScreenWidth() - context.resources.getDimensionPixelSize(R.dimen.size30dp)
        if (p.width >= context.resources.getDimensionPixelSize(R.dimen.size335dp)) {
            p.width = context.resources.getDimensionPixelSize(R.dimen.size335dp)
        }
        window!!.attributes = p     //设置生效
    }


    @LayoutRes
    protected abstract fun getLayoutId(): Int

    protected abstract fun initView()

    @SingleClick
    override fun onClick(v: View?) {

    }

    // 事件处理
    open fun initEvent() {}

    // 加载数据
    protected abstract fun loadData()
}